﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RibbonEditor.Rules;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.Forms.Rules;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    public class RuleDefinitions: IXmlBuildable
    {
        public TabDisplayRules TabDisplayRules { get; set; }
        public DisplayRules DisplayRules { get; set; }
        public EnableRules EnableRules { get; set; }

        public RuleDefinitions(XmlNode node)
        {
            try
            {
                if (node.SelectSingleNode("TabDisplayRules") != null)
                {
                    this.TabDisplayRules = new TabDisplayRules(node.SelectSingleNode("TabDisplayRules"));
                }

                if (node.SelectSingleNode("EnableRules") != null)
                {
                    this.EnableRules = new EnableRules(node.SelectSingleNode("EnableRules"), false);
                }

                if (node.SelectSingleNode("DisplayRules") != null)
                {
                    this.DisplayRules = new DisplayRules(node.SelectSingleNode("DisplayRules"), false);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ribbonDiffXmlNode)
        {
            try
            {
                XmlNode ruleDefinitionsXmlNode = XmlHelper.AddNode(ribbonDiffXmlNode, "RuleDefinitions");

                this.TabDisplayRules.AddNode(ruleDefinitionsXmlNode);
                this.DisplayRules.AddNode(ruleDefinitionsXmlNode);
                this.EnableRules.AddNode(ruleDefinitionsXmlNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("RuleDefinitions");
                node.ContextMenuStrip = new ContextMenuStrip();
                node.ContextMenuStrip.Tag = node;
                node.Tag = this;

                ContextMenuStripHelper.AddDisplayXmlMenu(node.ContextMenuStrip, false);

                this.TabDisplayRules.BuildTreeViewElement(node);
                this.EnableRules.BuildTreeViewElement(node);
                this.DisplayRules.BuildTreeViewElement(node);

                node.Expand();

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
